package controller;

import models.Audit;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import service.AdminService;
import service.AuditService;
import service.impl.AdminServiceImpl;
import service.impl.AuditServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class AuditController {

    AuditService auditService = new AuditServiceImpl();
    AdminService adminService = new AdminServiceImpl();
    @RequestMapping(value = "/audit", method = RequestMethod.GET)
    public String showLoginPage(HttpSession session) {

        String name = (String) session.getAttribute("username");
        if (adminService.isadmin(name))
            return "Audit";
        else return "main";
    }

    @RequestMapping(value = "/getAuditData",method = RequestMethod.POST)
    public void getAuditData(HttpServletResponse response) throws IOException {
        String result = auditService.getAuditList();
        response.getWriter().write(result);
    }

    @RequestMapping(value = "/returnAuditResult",method = RequestMethod.POST)
    public void returnAuditResult(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String id = request.getParameter("auditid");
        String result  = request.getParameter("result");
        Integer did = Integer.parseInt(id);
        Boolean answer = Boolean.parseBoolean(result);
        auditService.setDecision(did,answer);
    }

    @RequestMapping(value = "/submitAppeal",method = RequestMethod.POST)
    public void submitAppeal(HttpSession session)
    {
        String name = (String) session.getAttribute("username");

    }
}
